package vip.xiaonuo.core.indexMenu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.core.indexMenu.entity.IndexDataDetail;
import vip.xiaonuo.core.indexMenu.entity.MenuIndex;
import vip.xiaonuo.core.indexMenu.entity.MenuIndexAdd;
import vip.xiaonuo.core.indexMenu.entity.MenuIndexShowDto;
import vip.xiaonuo.core.indexMenu.mapper.CompanyIndexMapper;
import vip.xiaonuo.core.indexMenu.mapper.IndexDataDetailMapper;
import vip.xiaonuo.core.indexMenu.service.CompanyIndexService;


import java.util.Date;
import java.util.List;

@Service
public class CompanyIndexServiceImpl implements CompanyIndexService {

    @Autowired
    private CompanyIndexMapper companyIndexMapper;

    @Autowired
    private IndexDataDetailMapper indexDataDetailMapper;
    @Override
    public List<MenuIndex> menuIndexGet(String name) {

        return companyIndexMapper.menuIndexGet(name);
    }

    @Override
    public void menuIndexAdd(MenuIndexAdd menuIndexAdd) {
        if(menuIndexAdd.getId() != null){
            companyIndexMapper.menuIndexUpdate(menuIndexAdd);
        }else{
            companyIndexMapper.menuIndexAdd(menuIndexAdd);
        }
    }

    @Override
    public void menuIndexDel(Integer id) {
        companyIndexMapper.menuIndexDel(id);
    }

    @Override
    public PageInfo<IndexDataDetail> getIndexData(int page, int pageSize, Integer indexMenuId) {
        PageHelper.startPage(page, pageSize);
        PageInfo<IndexDataDetail> pageInfo = new PageInfo<>(indexDataDetailMapper.selectByIndexMenuId(indexMenuId));
        return pageInfo;
    }

    @Override
    public void addData(IndexDataDetail indexDataDetail) {
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        indexDataDetail.setCreateBy(loginUser.getId());
        indexDataDetailMapper.insert(indexDataDetail);
    }

    @Override
    public void updateData(IndexDataDetail indexDataDetail){
        indexDataDetailMapper.updateByPrimaryKey(indexDataDetail);
    }

    @Override
    public void deleteData(Integer id){
        indexDataDetailMapper.deleteByPrimaryKey(id);
    }

    public List<MenuIndexShowDto> getMenuIndexShowDto() {
        return companyIndexMapper.getMenuIndexShowDto();
    }
}
